import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
//获取文件的路径
import path from 'path'
//导入SVG-icons
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
//导入mock
import { viteMockServe } from 'vite-plugin-mock'
// https://vitejs.dev/config/

export default defineConfig(({ command, mode }) => {
  //获取各种环境下对应的变量
  //               环境文件  项目根目录
  let env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue(),
      //SVG-icons的配置
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      //mock的配置
      viteMockServe({
        // 指向mock下的文件
        mockPath: './mock/',
        //保证开发阶段可以使用mock接口
        localEnabled: command === 'serve',
        supportTs: true, // 是否使用mock接口；等于serve是开发阶段使用mock接口
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve('./src'), //相对路径别名配置，@代表src
      },
    },
    //scss全局变量的配置
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },
    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
})
